"""Test case for django_ldap.models"""

model_test = """
>>> from django_ldap.tests.fixtures import ldap_model

# model init test
>>> person = ldap_model(cn='testuser3',sn='test3')

# test django_ldap model apis
>>> person.pk == None
True
>>> person.save()
>>> person.id[:12]
'cn=testuser3'
>>> person.uid = 'test3'
>>> person.title = ['title1']
>>> person.save()
>>> ldap_model.objects.get(uid='test3').uid 
u'test3'
>>> person.title
[u'title1']
>>> person.title.append('title2')

# insert a new record with the same info should raise an error
>>> person2 = ldap_model(cn='testuser3',sn='test3')
>>> person2.save()
Traceback (most recent call last):
   ...
LdapBackendException: ALREADY_EXISTS ...

#@todo: value stored in django_ldap model should be in unicode form
#>>> person.title
#[u'title1', u'title2']
>>> person.save()
>>> ldap_model.objects.get(uid='test3').title
[u'title1', u'title2']
>>> person = ldap_model.objects.get(uid='test3')
>>> person.title=['title1']
>>> person.save()
>>> person.delete()

# make a instance without params should ok
>>> a = ldap_model()
>>> a.pk == None
True

# when init models with multiple rdns 
>>> class test_multi_rdn_model(ldap_model):
...   class Ldap:
...     rdn = ['uid', 'cn']
>>> a = test_multi_rdn_model(uid='test',cn='test')
>>> a.get_default_id()[:3]
'uid'
>>> b = test_multi_rdn_model(cn='test')
>>> b.get_default_id()[:2]
'cn'

# test add and remove relationship
>>> person1 = ldap_model.objects.get(cn='testuser1')
>>> person2 = ldap_model.objects.get(cn='testuser2')
>>> person1._add_relation(person2.pk, 'postalAddress')
>>> person2 = ldap_model.objects.get(cn='testuser2')
>>> person2.postalAddress[0][:12]
u'cn=testuser1'
>>> person1._del_relation(person2.pk, 'postalAddress')
>>> person2 = ldap_model.objects.get(cn='testuser2')
>>> person2.postalAddress
[]
>>> person1._add_relation(person2.pk, 'postalAddress', 'postalAddress', 'both')
>>> person1.postalAddress[0][:12]
u'cn=testuser2'
>>> person2 = ldap_model.objects.get(cn='testuser2')
>>> person2.postalAddress[0][:12]
u'cn=testuser1'
>>> person1._del_relation(person2.pk, 'postalAddress', 'postalAddress', 'both')
>>> person1.postalAddress
[]
>>> person2 = ldap_model.objects.get(cn='testuser2')
>>> person2.postalAddress
[]

# test model relationship
>>> person1 = ldap_model.objects.get(cn='testuser1')
>>> person1.postalAddress = ['sometext']
>>> person1.save()
>>> from django_ldap.fields import ManyToManyField
>>> class test_relation_model(ldap_model):
...   class Ldap:
...     group_attr = {'group': ManyToManyField(ldap_model, 'postalAddress', 'postalAddress', 'both')}
>>> relation_test_person = test_relation_model.objects.get(cn='testuser1')
>>> relation_test_person.group.add(person2.pk)
>>> person2 = ldap_model.objects.get(cn='testuser2')
>>> person2.postalAddress[0][:12]
u'cn=testuser1'
>>> person1 = ldap_model.objects.get(cn='testuser1')
>>> len(person1.postalAddress)
2
"""

person_test = """
>>> from django_ldap.tests.fixtures import ldap_model
>>> person = ldap_model(cn='testuser3',sn='test3')
>>> person.set_password('somepw')
>>> person.userPassword
[u'{MD5}uWhVfC75mBR1Qe+q2Z9B2g==']
>>> person.save()
>>> person = ldap_model.objects.get(cn='testuser3')
>>> person.check_password('somepw')
True
>>> person.check_password('badpw')
False
>>> person.set_password('somepw', 'clear')
>>> person.save()
>>> person.check_password('somepw')
True
>>> person.set_password('somepw', 'ssha')
>>> person.save()
>>> person.check_password('somepw')
True

# @todo: fix this test
#>>> person.set_password('somepw', 'crypt')
#>>> person.save()
#>>> person.check_password('somepw')
#True
>>> person.delete()
"""

__test__ = {
    'model_test': model_test,
    'person_test': person_test,
}